package com.lytoyo.neihanduanzi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lytoyo.neihanduanzi.mapper.BLogCollectMapper;
import com.lytoyo.neihanduanzi.model.Result;
import com.lytoyo.neihanduanzi.model.blog.BlogCollect;
import com.lytoyo.neihanduanzi.model.blog.BlogInfo;
import com.lytoyo.neihanduanzi.service.BlogCollectService;
import com.lytoyo.neihanduanzi.service.BlogService;
import com.rabbitmq.client.Return;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Package:com.lytoyo.neihanduanzi.service.impl
 *
 * @ClassName:BlogCollectServiceImpl
 * @Create:2025/3/16 12:41
 **/
@Slf4j
@Service
public class BlogCollectServiceImpl extends ServiceImpl<BLogCollectMapper, BlogCollect> implements BlogCollectService {

    @Resource
    private BlogService blogService;

    @Resource
    private BLogCollectMapper bLogCollectMapper;

    /**
     * 添加博客收藏
     *
     * @param blogId
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addCollectBlog(Long blogId, Long userId) {
        //查询该博客是否还存在
        BlogInfo one = this.blogService.getById(blogId);
        if (null == one) {
            return Result.fail(-1, "该博客已经不存在");
        }
        //查询是否已经收藏
        Long count = bLogCollectMapper.selectCount(new LambdaQueryWrapper<BlogCollect>().eq(BlogCollect::getBlogId, blogId).eq(BlogCollect::getUserId, userId));
        if (null != count && count > 0) {
            return Result.fail(-1, "数据异常");
        }
        //查询是否有过收藏，然后被取消的记录
        BlogCollect blogCollect = this.bLogCollectMapper.selectPastRecord(blogId, userId);
        //存在
        if (null != blogCollect) {
            //恢复收藏记录
            bLogCollectMapper.updatePastRecord(blogCollect.getId());
        } else {
            blogCollect = new BlogCollect().setBlogId(blogId).setUserId(userId);
            bLogCollectMapper.insert(blogCollect);
        }
        //增加博客收藏数量
        blogService.update(new LambdaUpdateWrapper<BlogInfo>().eq(BlogInfo::getId, blogId).setSql("collect = collect + 1"));
        return Result.success("收藏成功");
    }

    /**
     * 取消收藏博客
     *
     * @param blogId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result cacelCollectBlog(Long blogId, Long userId) {
        //查询是否有收藏记录
        Long count = this.bLogCollectMapper.selectCount(new LambdaQueryWrapper<BlogCollect>().eq(BlogCollect::getBlogId, blogId).eq(BlogCollect::getUserId, userId));
        if (null != count && count > 0) {
            //将该收藏记录取消
            this.remove(new LambdaQueryWrapper<BlogCollect>().eq(BlogCollect::getBlogId, blogId).eq(BlogCollect::getUserId, userId));
            //收藏记录-1
            this.blogService.update(new LambdaUpdateWrapper<BlogInfo>().eq(BlogInfo::getId, blogId).setSql("collect = collect - 1"));
            return Result.success("取消收藏");
        } else {
            return Result.fail(-1, "数据异常");
        }

    }
}
